<?php

namespace IDock\Insurance\ZFIC\Response;

use IDock\Http\Response;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Printer\NonCar\NonCarUnderwritingPrinter;
use IDock\Printer\PremiumUnderwritingPrinter;

class InsuranceConfirmResponse extends AllZFICResponse
{
    public function printerInit(): Response
    {
        $premiumUnderwritingPrinter = new PremiumUnderwritingPrinter();
        $nonCarUnderwritingPrinter = new NonCarUnderwritingPrinter();

        $data = $this->getBody();
        $tip = '';
        $reason = $data['proposalResHeadVM']['artificial'] ?? '';
        if (($data['state'] ?? '') != '200') {
            $tip = $data['msg'].' '.$reason;
            $premiumUnderwritingPrinter->statusCode = 'U4';
            $premiumUnderwritingPrinter->messageTip = $tip;
            $premiumUnderwritingPrinter->noCarUnderwritingPrinter = $nonCarUnderwritingPrinter;
            $this->printer = $premiumUnderwritingPrinter;
            return $this;
        }
        $data = $data['data'] ?? '';
        $premiumUnderwritingPrinter->statusCode = 'U3';
        $premiumUnderwritingPrinter->messageTip = $tip ?: ($data['proposalResHeadVM']['errorMessage'] ?? '');
        $premiumUnderwritingPrinter->messageTip .= ' '.$reason;
        $underwriteInd = $data['proposalResBodyVM']['underwriteInd'] ?? '';
        if (in_array($underwriteInd, ['1', '3', '5'])) {
            $premiumUnderwritingPrinter->statusCode = 'U1';//核保通过
        } else if ($underwriteInd == '2' or $underwriteInd == '0') {
            $premiumUnderwritingPrinter->statusCode = 'U2';//退回修改
            $premiumUnderwritingPrinter->messageTip .= $this->getMsg();
        } else if ($underwriteInd == 'A' or $underwriteInd == '8' or $underwriteInd == '9') {
            $premiumUnderwritingPrinter->statusCode = 'U3';//人工核保
        } else if ($underwriteInd == '4') {
            $premiumUnderwritingPrinter->statusCode = 'U6';//拒保
        } else if ($underwriteInd == '6') {
            $premiumUnderwritingPrinter->statusCode = 'U5';//已出保单
        } else if ($underwriteInd == '7') {
            $premiumUnderwritingPrinter->statusCode = 'U4';//复核失败
        }

        $premiumUnderwritingPrinter->ciInquiryNo = $data['proposalResBodyVM']['proposalNo0801'] ?? '';
        $premiumUnderwritingPrinter->biInquiryNo = $data['proposalResBodyVM']['proposalNo0808'] ?? '';
        $premiumUnderwritingPrinter->inquiryNo = $data['proposalResBodyVM']['proposalNo'] ?? '';

        //非车
        $proposalNoJY = $data['proposalResBodyVM']['proposalNoJY'] ?? '';

        if ($proposalNoJY) {
            $nonCarUnderwritingPrinter->statusCode = 'success';
        }
        $single = new NonCarInquirySinglePrinter();
        $single->statusCode = 'success';
        $single->inquiryNo = $proposalNoJY;
        $nonCarUnderwritingPrinter->nonCarSingleAppend($single);

        $premiumUnderwritingPrinter->noCarUnderwritingPrinter = $nonCarUnderwritingPrinter;

        $this->printer = $premiumUnderwritingPrinter;

        return $this;
    }
}
